package exam.axhu.junit;

import static org.junit.Assert.*;

import java.util.List;

import exam.axhu.entity.Course;
import exam.axhu.entity.CourseInfo;
import exam.axhu.entity.Teacher;
import exam.axhu.service.CourseService;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CourseTest {
	private static CourseService courseService;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception{
		try {
			ApplicationContext act = new ClassPathXmlApplicationContext("beans.xml");
			courseService =(CourseService) act.getBean("courseServiceImpl");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	
	@Test
	public void addcourse() {
		courseService.addCourse(new CourseInfo("CS101", "T1001", "计算机导论"));
		courseService.addCourse(new CourseInfo("CS102", "T1001", "计算机体系机构"));
		courseService.addCourse(new CourseInfo("CS103", "T1002", "操作系统"));
		
	}
	
	@Test
	public void deletecourse() {
		courseService.deleteCourse("C101");
		courseService.deleteCourse("C102");
		courseService.deleteCourse("C104");
		
		
	}
	
	
	@Test
	public void findcourse(){
		if(courseService.findByCourseID("CS102")!=null)
			System.out.println(courseService.findByCourseID("CS102").getCourseName());
		else System.out.println("NO");
	}
	
	
	@Test
	public void listcourse() {
		List<CourseInfo> ems = courseService.findByTeacherID("T1001");
		if(ems!=null)
			for(CourseInfo em : ems)
				System.out.println(em.getCourseID());
		else System.out.println("NO");
	}
	
	@Test
	public void addstudent(){
		courseService.addStudent("CS101","S1001");
		courseService.addStudent("CS101","S1002");
		courseService.addStudent("CS101","S1003");
		
		
	}
	
	@Test
	public void deletestudent(){
		
		courseService.deleteStudent("CS103","S1003");
	}

	@Test
	public void search(){
		if(courseService.searchStudent("CS102","S1002")!=null)
			 System.out.println(courseService.searchStudent("CS102","S1002").getStudentID());
		else  System.out.println("NO");
	}
	
	@Test
	public void listStudents(){
		List<Course> courses = courseService.listStudentsByCourseID("CS101");
		for(Course course:courses)
			System.out.println(course.getStudentID());
	}
	
	@Test
	public void listCourses(){
		List<Course> courses = courseService.listCoursesByStudentID("S1001");
		for(Course course:courses)
			System.out.println(course.getCourseID());
	}
	
}
